package com.study_security.service.impl;
import com.lowagie.text.*;
import com.lowagie.text.Font;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
import com.study_security.entity.Project;
import com.study_security.entity.SysLog;
import com.study_security.service.ExportService;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;
import java.awt.Color;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.awt.*;

@Service
public class ExportServiceImpl  implements ExportService {

    public byte[] exportProjectsToExcel(List<Project> projects) throws IOException {
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("Projects");

        // 创建表头
        Row headerRow = sheet.createRow(0);
        String[] headers = {"项目ID", "项目名", "任务进度", "审核进度", "负责人ID", "开始日期", "结束日期", "预算", "合同金额", "重要级别", "项目类型", "项目状态"};
        for (int i = 0; i < headers.length; i++) {
            Cell cell = headerRow.createCell(i);
            cell.setCellValue(headers[i]);
        }

        // 填充数据
        int rowNum = 1;
        for (Project project : projects) {
            Row row = sheet.createRow(rowNum++);
            row.createCell(0).setCellValue(project.getProjectId());
            row.createCell(1).setCellValue(project.getProjectName());
            row.createCell(2).setCellValue(project.getTaskProgress().toString());
            row.createCell(3).setCellValue(project.getAuditProgress().toString());
            row.createCell(4).setCellValue(project.getResponsibleId());
            row.createCell(5).setCellValue(project.getStartDate().toString());
            row.createCell(6).setCellValue(project.getEndDate().toString());
            row.createCell(7).setCellValue(project.getBudget().toString());
            row.createCell(8).setCellValue(project.getContractAmount().toString());
            row.createCell(9).setCellValue(project.getImportantLevel());
            row.createCell(10).setCellValue(project.getProjectType());
            row.createCell(11).setCellValue(project.getProjectStatus());
        }

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        workbook.write(outputStream);
        workbook.close();

        return outputStream.toByteArray();
    }
    @Override
    public byte[] exportLogsToExcel(List<SysLog> logs) throws IOException {
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("SysLogs");

        // 创建表头
        Row headerRow = sheet.createRow(0);
        String[] headers = {"日志ID", "用户ID","用户姓名", "操作类型", "请求URL", "方法", "参数", "响应时间(ms)", "状态", "创建时间"};
        for (int i = 0; i < headers.length; i++) {
            Cell cell = headerRow.createCell(i);
            cell.setCellValue(headers[i]);
        }

        // 设置日期格式
        CellStyle dateCellStyle = workbook.createCellStyle();
        CreationHelper createHelper = workbook.getCreationHelper();
        short dateFormat = createHelper.createDataFormat().getFormat("yyyy-MM-dd HH:mm:ss");
        dateCellStyle.setDataFormat(dateFormat);

        // 填充数据
        int rowNum = 1;
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        for (SysLog log : logs) {
            Row row = sheet.createRow(rowNum++);
            row.createCell(0).setCellValue(log.getId() != null ? log.getId() : 0);
            row.createCell(1).setCellValue(log.getUserId() != null ? log.getUserId() : 0);
            row.createCell(2).setCellValue(log.getUserName() != null ? log.getUserName() : "");
            row.createCell(3).setCellValue(log.getOperationType() != null ? log.getOperationType() : "");
            row.createCell(4).setCellValue(log.getRequestUrl() != null ? log.getRequestUrl() : "");
            row.createCell(5).setCellValue(log.getMethod() != null ? log.getMethod() : "");
            row.createCell(6).setCellValue(log.getParams() != null ? log.getParams() : "");
            row.createCell(7).setCellValue(log.getResponseTime() != null ? log.getResponseTime() : 0);
            row.createCell(8).setCellValue(log.getStatus() != null ? log.getStatus() : "");

            Cell dateCell = row.createCell(9);
            if (log.getCreatedAt() != null) {
                dateCell.setCellValue(log.getCreatedAt().format(formatter));
            } else {
                dateCell.setCellValue("");
            }
            dateCell.setCellStyle(dateCellStyle);
        }

        // 自动调整列宽
        for (int i = 0; i < headers.length; i++) {
            sheet.autoSizeColumn(i);
        }

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        workbook.write(outputStream);
        workbook.close();

        return outputStream.toByteArray();
    }
}
